package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;


import android.widget.TextView;

import com.chainyoung.common.adpter.BaseFragmentAdapter;

import com.chainyoung.common.appcommon.entity.HdWalletCache;

import com.chainyoung.common.base.BaseActivity;

import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;

import com.chainyoung.common.jsoup.interfaces.Text;
import com.chainyoung.common.utils.ClipboardUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;

import com.chainyoung.component_tmperson.ui.fragment.HdWalletTokenListFragment;
import com.chainyoung.component_tmperson.ui.fragment.HdWalletTransactionListFragment;


import com.flyco.tablayout.SlidingTabLayout;


import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * HD 钱包
 */
public class HdWalletActivity extends BaseActivity {

    @BindView(R2.id.iv_logo)
    ImageView ivLogo;
    @BindView(R2.id.tv_num)
    TextView tvNum;
    @BindView(R2.id.tv_price_cny)
    TextView tvPriceCny;
    @BindView(R2.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.tv_addr)
    TextView tvAddr;


    //private HdWallet hdWallet;
    private HdWalletCache hdWallet;

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }


    @Override
    protected void initUI() {
        ClipboardUtil.init(mContext);
        hdWallet = (HdWalletCache) getIntent().getSerializableExtra("hdwallet");
        if (hdWallet != null) {
            initView();
        }
        //  initRecyclerList();
    }

    @Override
    protected boolean useDefaultStatusBar() {
        return false;
    }

    private void initView() {
        setCurrentTitle(hdWallet.getWalletName());
        mCurrentTtile.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        mBarLeftImageView.setImageResource(R.mipmap.ic_back_white);
        toolbar.setBackgroundColor(ContextCompat.getColor(mContext, R.color._303e63));
        //tvAddr.setText(hdWallet.getAddress());
        tvNum.setText(hdWallet.getBalance()+" "+hdWallet.getCoinType().toUpperCase());
        ivLogo.setImageResource(R.mipmap.icon_eth);
        tvPriceCny.setText("≈CNY " + hdWallet.getBanlanceCny());
        showAndSetBarRightText("更多");
        mBarRightTextView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        tvAddr.setText(hdWallet.getAddress().substring(0, 10) + "..." + hdWallet.getAddress().substring(hdWallet.getAddress().length() - 16));
        Drawable drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_copy);
      //  DrawableCompat.setTint(drawable, Color.parseColor("#6085a1"));
        tvAddr.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);
        tvAddr.setCompoundDrawablePadding(18);
        initTabLayout();
    }

    private void initTabLayout() {
        fragmentList.add(HdWalletTransactionListFragment.newInstance(hdWallet.getAddress()));
        fragmentList.add(HdWalletTokenListFragment.newInstance(hdWallet.getAddress(), hdWallet));
        titles.add("交易记录");
        titles.add("合约代币");
        //tabLayout.setTabMode(TabLayout.MODE_FIXED);
        viewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList, titles));
        tabLayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(titles.size());
    }


    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_hdwallet1;
    }


    @Override
    public void onRightTxtClicked(View v) {
        super.onRightTxtClicked(v);
        Intent intent = new Intent(mContext, HdWalletSettingActivity.class);
        intent.putExtra("hdwallet", hdWallet);
        mContext.startActivity(intent);
    }

    @OnClick(R2.id.tv_send)
        //发送
    void send() {
        Intent intent = new Intent(mContext, HdWalletSendTranscationActivity.class);
        intent.putExtra("hdWallet", hdWallet);
        startActivity(intent);
    }


    @OnClick(R2.id.tv_recieve)
    void recieve() {
        Intent intent = new Intent(mContext, HdWalletCodeActivity.class);
        intent.putExtra("hdWallet", hdWallet);
        startActivity(intent);
    }

    @OnClick(R2.id.tv_addr)
    void copyAddr() {
        ClipboardUtil.getInstance().copyText("block", hdWallet.getAddress());
        ToastUtil.showToast(getString(R.string.tmperson_addr_copy_success));
    }


    @Subscriber
    public void onRefreshName(BaseEvent event) {
        if (event.getCode() == BaseEvent.EventCode.REFRESH_WALLET_NAME) {
            HdWalletCache hdWallet = (HdWalletCache) event.getData();
            setCurrentTitle(hdWallet.getWalletName());
        }
    }
}
